package zhihui.mobile.ui;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

/**
 * Created by 张公铭 on 2017/7/5 0005.
 */

public class FragmentFactory
{

    public final static String ADDRESS_FRAGMENT = "address_fragment";
    public final static String PHONE_FRAGMENT = "phone_fragment";
    public final static String PHONE_CODE_FRAGMENT = "phone_code_fragment";
    public final static String PASSWORD_FRAGMENT = "password_fragment";

    /**
     * 创建一个Fragment, 如果系统中有这个实例, 直接查找到用已经存在的
     *
     * @param parent
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T extends Fragment> T create(FragmentActivity parent, Class<T> c, String tag)
    {
        T f = null;
        FragmentManager fm = parent.getSupportFragmentManager();
        Fragment existFgt = fm.findFragmentByTag(tag);
        if (existFgt != null)
        {
            f = (T) existFgt;
        }
        else
        {
            try
            {
                f = c.newInstance();
            }
            catch (Exception e)
            {

            }
        }
        return f;
    }
}
